package com.shopping.common.core.object;

import com.shopping.common.core.constant.ErrorCodeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @program: shopping-mall
 * @ClassName ResponseResult
 * @description: 返回结果封装类
 * @author: zlw
 * @create: 2024-04-07 22:24
 * @Version 1.0
 **/

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResponseResult<T> {

    /**
     * 为了优化性能 所有没有携带数据的正确结果,均可用该对象表示
     */
    private  static final ResponseResult<Void> OK  = new ResponseResult<>();

    /**
     * 是否成功标记
     */
    private boolean success = true;

    /**
     * 返回的状态码
     */
    private String code = "SUCCESS";

    /**
     * 返回的消息提示
     */
    private String message = "成功";

    /**
     * 返回的数据
     */
    private  T data;


    /**
     * 构造方法
     */
    public ResponseResult(String errCode,String errorMessage){
        this.success = false;
        this.code = errCode;
        this.message = errorMessage;
    }

    /**
     * 带返回结果的对象
     * @param data
     * @return
     * @param <T>
     */
    public static <T> ResponseResult<T> success(T data){
//        ResponseResult<T> responseResult = new ResponseResult<>();
//        responseResult.setData(data);
//        responseResult.setCode(200);
//        responseResult.setMessage("成功");
        ResponseResult<T> resp = new ResponseResult<>();
        resp.data = data;
        return resp;
    }

    public static ResponseResult<Void> success(){
        return OK;
    }

    public static ResponseResult<Void> error(ErrorCodeEnum errorCodeEnum){
        return error(errorCodeEnum.name(),errorCodeEnum.getErrorMessage());
    }

    public static <T> ResponseResult<T> error(ErrorCodeEnum errorCodeEnum,String errorMessage){
        return error(errorCodeEnum.name(),errorMessage);
    }

    public static <T> ResponseResult<T> success(String message,T data){
        ResponseResult<T> resp = new ResponseResult<>();
        resp.data = data;
        resp.message = message;
        return resp;
    }

    public static <T> ResponseResult<T> success(String code,String message,T data){
        ResponseResult<T> resp = new ResponseResult<>();
        resp.code = code;
        resp.message = message;
        resp.data = data;
        return resp;
    }


    public static ResponseResult<Void> success(String code,String message){
        ResponseResult<Void> resp = new ResponseResult<>();
        resp.code = code;
        resp.message = message;
        return resp;
    }

    public static <T> ResponseResult<T> error(String errorCode,String errorMessage){
        return new ResponseResult<>(errorCode,errorMessage);
    }

    /**
     * 多参error
     * @param errorCode
     * @param errorMessage
     * @param data
     * @return
     * @param <T>
     */
    public static <T> ResponseResult<T> error(String errorCode, String errorMessage,T data){
        ResponseResult<T> resp = new ResponseResult<>(errorCode,errorMessage);
        resp.data = data;
        return resp;
    }

    public static <T> ResponseResult<T> error(ErrorCodeEnum errorCodeEnum, T data){
        ResponseResult<T> resp = new ResponseResult<>();
        resp.data = data;
        resp.code = errorCodeEnum.name();
        resp.message = errorCodeEnum.getErrorMessage();
        return resp;
    }
}
